package io.sdph.filters;

import io.sdph.core.ComponentContainer;
import io.sdph.userprofile.ISessionManager;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class LoginFilter implements Filter {
	
	private static final Logger log = Logger.getLogger(LoginFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		ISessionManager sessionManager = ComponentContainer.getInstance().getSessionManager();
		if (sessionManager == null || sessionManager.getCurrentUser() == null){
			((HttpServletResponse) response).sendRedirect("/login.html");
			log.error("User is not authorized!");
		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		
	}

}
